﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeVisual
{
    public partial class SettingDialog : Form
    {
        public SettingDialog()
        {
            InitializeComponent();
            InitSettings();
        }

        void InitSettings()
        {
            string val = null;
            
            // Analyzovat len subory z projektu
            val = Setting.GetSetting("OnlyFromProject");
            if (val == "1") checkBox1.Checked = true;


            // Vystupny format
            val = Setting.GetSetting("OutputFormat");

            switch (val)
            {
                case "PDF":
                    comboBox1.SelectedIndex = 0;
                    break;
                case "JPG":
                    comboBox1.SelectedIndex = 1;
                    break;
                case "HTML":
                    comboBox1.SelectedIndex = 2;
                    break;
                default:
                    comboBox1.SelectedIndex = 0;
                    break;
            }

            // Ignorovat nepouzite premenne
            val = Setting.GetSetting("IgnoreUnusedVar");
            if (val == "1") checkBox2.Checked = true;

            // Ignorovat nepouzite funkcie
            val = Setting.GetSetting("IgnoreUnusedFunc");
            if (val == "1") checkBox3.Checked = true;

            // Ignorovat funkcie, ktore su pouzite viac/menej ako nejaky pocet volani
            val = Setting.GetSetting("NumOfCallFunc");
            if (val != "none")
            {
                checkBox4.Checked = true;
                if (val[0] == '+')
                {
                    comboBox2.SelectedIndex = 0;
                }
                else
                {
                    comboBox2.SelectedIndex = 1;
                }

                //numericUpDown1.Value = Convert.ToDecimal(val.Substring(1));
            } 
            
            // Ignorovat premenne, ktore su pouzite viac/menej ako nejaky pocet pouziti
            val = Setting.GetSetting("NumOfChangeVar");
            if (val != "0")
            {
                checkBox5.Checked = true;
                if (val[0] == '+')
                {
                    comboBox3.SelectedIndex = 0;
                }
                else
                {
                    comboBox3.SelectedIndex = 1;
                }

                numericUpDown2.Value = Convert.ToDecimal(val.Substring(1));
            }

            // Cesta na export
            val = Setting.GetSetting("ExportPath");
            label2.Text = val;

            // Otvorenie suboru po exporte
            val = Setting.GetSetting("OpenAfterExport");
            if (val == "1") checkBox6.Checked = true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult res = folderBrowserDialog1.ShowDialog();
            if (res == DialogResult.OK)
            {
                label2.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveSetting();
            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void SaveSetting()
        {
            string val = null;

            // Analyzovat len subory z projektu
            if (checkBox1.Checked)
                Setting.SetSetting("OnlyFromProject", "1");
            else
                Setting.SetSetting("OnlyFromProject", "1");

            // Vystupny format
            val = Setting.GetSetting("OutputFormat");

            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    Setting.SetSetting("OutputFormat", "PDF");
                    break;
                case 1:
                    Setting.SetSetting("OutputFormat", "SVG");
                    break;
                case 2:
                    Setting.SetSetting("OutputFormat", "HTML");
                    break;
            }

            // Ignorovat nepouzite premenne
            if (checkBox2.Checked) Setting.SetSetting("IgnoreUnusedVar", "1");
            else Setting.SetSetting("IgnoreUnusedVar", "0");

            // Ignorovat nepouzite funkcie
            if (checkBox3.Checked) Setting.SetSetting("IgnoreUnusedFunc", "1");
            else Setting.SetSetting("IgnoreUnusedFunc", "0");

            // Ignorovat funkcie, ktore su pouzite viac/menej ako nejaky pocet volani
            if (checkBox4.Checked)
            {
                string s = null;

                if (comboBox2.SelectedIndex == 0)
                    s = "+";
                else
                    s = "-";

                s += numericUpDown1.Value.ToString();

                Setting.SetSetting("NumOfCallFunc", s);
            }
            else
            {
                Setting.SetSetting("NumOfCallFunc", "none");
            }

            // Ignorovat premenne, ktore su pouzite viac/menej ako nejaky pocet pouziti
            if (checkBox5.Checked)
            {
                string s = null;

                if (comboBox3.SelectedIndex == 0)
                    s = "+";
                else
                    s = "-";

                s += numericUpDown2.Value.ToString();

                Setting.SetSetting("NumOfModifVar", s);
            }
            else
            {
                Setting.SetSetting("NumOfModifVar", "none");
            }


            // Cesta na export
            Setting.SetSetting("ExportPath", label2.Text);


            // Otvorenie suboru po exporte
            if (checkBox6.Checked) Setting.SetSetting("OpenAfterExport", "1");
            else
                Setting.SetSetting("OpenAfterExport", "0");

        }
    }
}
